using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Media;
using Common;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using SExperiment;
using SExperiment.ExperimentResult;
using SSettings;
using SWellPlate;

namespace MainView.MainControls
{
	public class UCWellInfo_AmoyDx : UCWellInfo
	{
		private static string Key_SampleInfo = "Key_wellinfo_sample";

		private static string Key_WellInfo = "Key_wellinfo_well";

		private static string Key_Ct = "Key_wellinfo_ct";

		private static string Key_Result = "Key_wellinfo_result";

		private static string Key_TargetResult = "Key_wellinfo_target_result";

		public UCWellInfo_AmoyDx()
		{
			base.GridColumns = new GridColumns
			{
				Columns = 
				{
					new ColumnInfo
					{
						FieldName = Key_SampleInfo,
						Header = Utility.GetMessage("wellinfo_sample"),
						Info = 2,
						Display = true,
						AllowDelete = false,
						AllowEditing = false
					},
					new ColumnInfo
					{
						FieldName = Key_WellInfo,
						Header = Utility.GetMessage("wellinfo_well"),
						Info = 2,
						Display = true,
						AllowDelete = false,
						AllowEditing = false
					},
					new ColumnInfo
					{
						FieldName = Key_Ct,
						Header = Utility.GetMessage("wellinfo_ct"),
						Info = 2,
						Display = true,
						AllowDelete = false,
						AllowEditing = false
					},
					new ColumnInfo
					{
						FieldName = Key_TargetResult,
						Header = Utility.GetMessage("target_result"),
						Info = 2,
						Display = true,
						AllowDelete = false,
						AllowEditing = false
					},
					new ColumnInfo
					{
						FieldName = Key_Result,
						Header = Utility.GetMessage("wellinfo_result"),
						Info = 2,
						Display = true,
						AllowDelete = false,
						AllowEditing = false
					},
					new ColumnInfo
					{
						FieldName = "SampleTargetItem.ChannelNo",
						Header = Utility.GetMessage("Channel"),
						Info = 1,
						Display = false,
						AllowDelete = false,
						AllowEditing = false
					}
				}
			};
			base.AutoWidth = true;
		}

		protected override void InsertNewColumn(string key, int index, string header, bool visible)
		{
			if (key == "SampleTargetItem.ChannelNo")
			{
				base.InsertNewColumn(key, index, header, false);
				return;
			}
			UltraGridColumn ultraGridColumn = (from UltraGridColumn s in ultraGrid.DisplayLayout.Bands[0].Columns
				where s.Key == key
				select s).FirstOrDefault();
			if (ultraGridColumn == null)
			{
				informationTable.Band.Columns.Add(key, typeof(string));
				ultraGridColumn = (from UltraGridColumn s in ultraGrid.DisplayLayout.Bands[0].Columns
					where s.Key == key
					select s).FirstOrDefault();
				ultraGridColumn.FilterClearButtonVisible = DefaultableBoolean.False;
				ultraGridColumn.MergedCellContentArea = MergedCellContentArea.VirtualRect;
				ultraGridColumn.MergedCellEvaluator = new CustomMergedCellEvaluator_AmoyDx(view);
				ultraGridColumn.MergedCellEvaluationType = MergedCellEvaluationType.MergeSameValue;
				ultraGridColumn.MergedCellStyle = ((!(key == Key_Ct)) ? MergedCellStyle.Always : MergedCellStyle.Never);
				ultraGridColumn.AllowRowFiltering = (shouldFilter ? DefaultableBoolean.True : DefaultableBoolean.False);
				ultraGridColumn.CellAppearance.TextHAlign = HAlign.Left;
				if (key == Key_SampleInfo)
				{
					ultraGridColumn.CellAppearance.TextHAlign = HAlign.Center;
					ultraGridColumn.MinWidth = 100;
				}
				else if (key == Key_WellInfo)
				{
					ultraGridColumn.CellAppearance.TextHAlign = HAlign.Center;
					ultraGridColumn.MinWidth = 100;
				}
				else if (key == Key_Ct)
				{
					ultraGridColumn.CellAppearance.TextHAlign = HAlign.Left;
					ultraGridColumn.MinWidth = 100;
				}
				else if (key == Key_Result)
				{
					ultraGridColumn.CellAppearance.TextHAlign = HAlign.Left;
				}
				else if (key == Key_TargetResult)
				{
					ultraGridColumn.CellAppearance.TextHAlign = HAlign.Right;
					ultraGridColumn.MinWidth = 140;
				}
				ultraGridColumn.Header.Caption = header;
			}
			if (key == Key_Result)
			{
				List<Sample> list = (from s in base.Subset.Wells
					where s.Sample != null
					select s.Sample).ToList();
				bool flag = false;
				foreach (Sample item in list)
				{
					foreach (Conclusion conclusion in item.Result.ConclusionList)
					{
						if (conclusion.Color != Colors.Black)
						{
							flag = true;
							break;
						}
					}
					if (flag)
					{
						break;
					}
				}
				if (flag)
				{
					ultraGridColumn.Style = ColumnStyle.FormattedText;
				}
				else
				{
					ultraGridColumn.Style = ColumnStyle.Default;
				}
			}
			else if (key == Key_TargetResult)
			{
				List<ObservableCollection<SampleTargetItem>> list2 = (from s in base.Subset.Wells
					where s.Sample != null
					select s.Sample.Items).ToList();
				bool flag2 = false;
				foreach (ObservableCollection<SampleTargetItem> item2 in list2)
				{
					foreach (SampleTargetItem item3 in item2)
					{
						foreach (Conclusion conclusion2 in item3.Result.ConclusionList)
						{
							if (conclusion2.Color != Colors.Black)
							{
								flag2 = true;
								break;
							}
						}
						if (flag2)
						{
							break;
						}
					}
					if (flag2)
					{
						break;
					}
				}
				if (flag2)
				{
					ultraGridColumn.Style = ColumnStyle.FormattedText;
				}
				else
				{
					ultraGridColumn.Style = ColumnStyle.Default;
				}
			}
			ultraGridColumn.Header.VisiblePosition = index;
			ultraGridColumn.Hidden = !visible;
		}

		protected override object GetColumnData(string key, ChartData chartData, bool displayData = true)
		{
			if (key == "SampleTargetItem.ChannelNo")
			{
				return base.GetColumnData(key, chartData, displayData);
			}
			StringBuilder stringBuilder = new StringBuilder();
			if (key == Key_SampleInfo)
			{
				if (!string.IsNullOrEmpty(chartData.WellCell.Well.Sample.SampleName))
				{
					stringBuilder.Append(chartData.WellCell.Well.Sample.SampleName);
					stringBuilder.Append("-");
				}
				stringBuilder.Append(chartData.WellCell.Well.Project.BasicInfo.Name);
				stringBuilder.Append("-");
				stringBuilder.Append(Utility.GetMessage(chartData.SampleTargetItem.SampleType.ToString()));
				if (!string.IsNullOrEmpty(chartData.WellCell.Well.Sample.PatientInfo.Remark))
				{
					stringBuilder.Append("-");
					stringBuilder.Append(chartData.WellCell.Well.Sample.PatientInfo.Remark);
				}
			}
			else
			{
				if (key == Key_WellInfo)
				{
					return chartData.WellCell.Well.CellName + ":" + chartData.WellCell.Well.TubeName;
				}
				if (key == Key_Ct)
				{
					stringBuilder.Append(chartData.SampleTargetItem.Dye + ":");
					if (!double.IsNaN(chartData.SampleTargetItem.Result.CtValue))
					{
						if (double.IsPositiveInfinity(chartData.SampleTargetItem.Result.CtValue))
						{
							stringBuilder.Append(Utility.GetMessage("NoCt"));
						}
						else
						{
							stringBuilder.Append(chartData.SampleTargetItem.Result.CtValue.ToString("#,0.00"));
						}
					}
				}
				else if (key == Key_TargetResult)
				{
					foreach (Conclusion conclusion in chartData.SampleTargetItem.Result.ConclusionList)
					{
						UltraGridColumn ultraGridColumn = (from UltraGridColumn s in ultraGrid.DisplayLayout.Bands[0].Columns
							where s.Key == key
							select s).FirstOrDefault();
						if (ultraGridColumn != null && ultraGridColumn.Style == ColumnStyle.Default)
						{
							stringBuilder.Append(conclusion.Content + " ");
						}
						else
						{
							stringBuilder.Append(displayData ? ConvertConclusionToHtml(conclusion) : (conclusion.Content + " "));
						}
					}
					if (!displayData && stringBuilder.Length > 0)
					{
						stringBuilder.Remove(stringBuilder.ToString().Length - 1, 1);
					}
				}
				else if (key == Key_Result)
				{
					foreach (Conclusion conclusion2 in chartData.WellCell.Well.Sample.Result.ConclusionList)
					{
						UltraGridColumn ultraGridColumn2 = (from UltraGridColumn s in ultraGrid.DisplayLayout.Bands[0].Columns
							where s.Key == key
							select s).FirstOrDefault();
						if (ultraGridColumn2 != null && ultraGridColumn2.Style == ColumnStyle.Default)
						{
							stringBuilder.Append(conclusion2.Content + " ");
						}
						else
						{
							stringBuilder.Append(displayData ? ConvertConclusionToHtml(conclusion2) : (conclusion2.Content + " "));
						}
					}
					if (!displayData && stringBuilder.Length > 0)
					{
						stringBuilder.Remove(stringBuilder.ToString().Length - 1, 1);
					}
				}
			}
			return stringBuilder.ToString();
		}
	}
}
